<template>
    <span class="TimeCount" style="margin-right:5px">
        距上次刷新：{{ str_time }}
    </span>
</template>

<script>
export default {
    name: 'TimeCount',
    data() {
        return {
            timer: null,
            str_time: '',
            speed: 0,
        }
    },
    created() {
        clearTimeout(this.timer);
        this.timer = null;
        this.onTimeCount();
    },
    methods: {
        onTimeCount() {
            if(this.timer) {
                clearTimeout(this.timer);
                this.timer = null;
            }

            this.timer = setTimeout(() => {
                this.speed += 1;
                this.str_time = this.onTransfromTime();
                this.onTimeCount();
            }, 1000)
        },
        onTransfromTime() {
            let speed = this.speed;
            console.log('speed',speed)
            if(speed >= 60) {
                let fen = parseInt(speed / 60);
                let Hour = ''
                if(fen >= 60) {
                    Hour = (parseInt(fen / 60));
                    fen = fen % 60;
                   
                }
                return Hour ? `${Hour}小时${fen}` : `${fen}分前`;
            }  else {
                return speed + '秒前'
            }
            
        },
        clearSpeed() {
            this.speed = 0;
            clearTimeout(this.timer);
            this.timer = null;
            this.str_time = '0秒'
            this.onTimeCount();
        }

    }

}
</script>

<style lang="scss" scoped>

</style>